package com.ruoyi.student.impl;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.student.IStudentUserService;
import com.ruoyi.student.vo.UserInfoVO;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class StudentUserService implements IStudentUserService {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysPostService postService;

    @Override
    public UserInfoVO getUserInfo(long uid) {
        SysUser sysUser = userService.selectUserById(uid);


        UserInfoVO info = new UserInfoVO();
        info.setUid(sysUser.getUserId());
        info.setNickname(sysUser.getNickName());
        info.setUserName(sysUser.getUserName());
        Date birthday = sysUser.getBirthday();

        info.setPostId(sysUser.getPostId());
        info.setPostName(sysUser.getPostName());

        info.setDeptId(sysUser.getDeptId());
        info.setDeptName(sysUser.getDeptName());

        info.setAvatar(sysUser.getAvatar());

        if (StringUtils.isEmpty(info.getAvatar())) {
            info.setAvatar("/static/img/profile.473f5971.jpg");
        }

        if(birthday != null){
            info.setAge(DateUtil.age(birthday ,new Date()));
        }
//        setDept(sysUser ,info);


        return info;
    }


//    private void setDept(SysUser sysUser ,UserInfoVO info){
//        if(sysUser.getDeptId() != null){
//            SysDept dept = deptService.selectDeptById(sysUser.getDeptId());
//            info.setDept(dept);
//        }
////        SysDept empty = new SysDept();
////        SysDept classs = sysUser.getDeptId() != null ?  deptService.selectDeptById(sysUser.getDeptId()) : empty; //班级
////        if(classs == null){
////            classs = empty;
////        }
////        SysDept college = classs.getParentId() != null ? deptService.selectDeptById(classs.getParentId()) : empty; // 院校
////        if(college == null){
////            college = empty;
////        }
////        SysDept school =  college.getParentId() != null ? deptService.selectDeptById(college.getParentId()) : empty; // 学校
////        if(school == null){
////            school = empty;
////        }
////        info.setSchool(school.getDeptName());
////        info.setCollege(college.getDeptName());
////        info.setClasss(classs.getDeptName());
//    }

    @Override
    public List<UserInfoVO> getUserInfo(List<Long> list) {
        Long[] uids = list.stream().toArray(Long[]::new);
        List<SysUser> userList = userService.selectUserByIds(uids);

        List<Long> deptIdList = userList.stream().map(u -> u.getDeptId()).collect(Collectors.toList());
        List<SysDept> depts =  deptService.selectDeptList(deptIdList);

        deptIdList.clear();
        for(SysDept dept : depts){
            String[] pids = dept.getAncestors().split(",");
            for(String id : pids){
                deptIdList.add(Long.parseLong(id));
            }
        }
        List<SysDept> tmp = deptService.selectDeptList(deptIdList);
        depts.addAll(tmp);
        Map<Long ,SysDept> map = depts.stream().collect(Collectors.toMap(d -> d.getDeptId() , d ->d));

        List<UserInfoVO> infoVOS = new ArrayList<>(userList.size());
        for(SysUser user : userList){
            UserInfoVO info = new UserInfoVO();
            info.setUid(user.getUserId());
            info.setNickname(user.getNickName());
            Date birthday = user.getBirthday();
            if(birthday != null){
                info.setAge(DateUtil.age(birthday ,new Date()));
            }

            SysDept dept = map.get(user.getDeptId());
            info.setDept(dept);
//            String[] schoolInfo = getSchoolInfo(map ,user.getDeptId());
//            info.setSchool(schoolInfo[0]);
//            info.setCollege(schoolInfo[1]);
//            info.setClasss(schoolInfo[2]);

            infoVOS.add(info);
        }

        return infoVOS;
    }

//    private String[] getSchoolInfo(Map<Long ,SysDept> map ,long deptId){
//        String[] info = new String[3];
//        SysDept dept = map.get(deptId);
//        if(dept != null) {
//            info[2] = dept.getDeptName();
//            String[] pids = dept.getAncestors().split(",");
//            if(pids.length >= 1){
//                info[1] = getDeptName(map ,Long.parseLong(pids[pids.length - 1])); // college
//            }
//            if(pids.length >= 2) {
//                info[0] = getDeptName(map, Long.parseLong(pids[pids.length - 2])); // school
//            }
//        }
//        return info;
//    }

    private String getDeptName(Map<Long ,SysDept> map ,long deptId){
        SysDept dept = map.get(deptId);
        if(dept != null){
            return dept.getDeptName();
        }
        return "";
    }
}
